package yifeng.gui;

import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;

public class Elevator extends JFrame implements ActionListener{

	/**
	 * how much floors
	 */
	private int floors;
	private final int floorHeight = 40;
	private final int floorWidth = 50;

	private final int frameWidth = 400;
	private final int frameHeight = 600;
	
	/**
	 * floor object list
	 */
	private List<JButton> floorList = new ArrayList<JButton>();
	/**
	 * control object list
	 */
	private List<JButton> buttonList = new ArrayList<JButton>();
	public int getFloors() {
		return floors;
	}

	private List<JButton> upList = new ArrayList<JButton>();
	private List<JButton> downList = new ArrayList<JButton>();
	
	private int currentFloor = 0;
	private Direction direction = Direction.stop;
	private int[] floorCommands;
	private int[] upCommands;
	private int[] downCommands;
	enum Direction{up,down,stop}
	
	//the first floor's left top position
	private int startX = 90;
	private int startY = frameHeight - 200;
	
	public Elevator(int n) {
		super();
		this.floors = n;
		floorCommands = new int[floors];
		upCommands = new int[floors];
		downCommands = new int[floors];
		this.setSize(frameWidth, frameHeight);
		this.getContentPane().setLayout(null);// 差别很大

		createFloors(n);
		floorList.get(currentFloor).setEnabled(true);
		createControlButtons();
		
		new Controller(this).start();
//		new Mover(this).start();
	}

	private void createFloors(int n) {
		for (int i = 1; i <= n; i++) {
			JButton b = new JButton(i + "");
			b.setMargin(new Insets(0, 0, 0, 0));

			this.add(b);
			floorList.add(b);
			int xPixel = startX;
			int yPixel = startY - (i - 1) * floorHeight;
			b.setBounds(xPixel, yPixel, floorWidth, floorHeight);
			b.setEnabled(false);
			createUpAndDownButton(xPixel, yPixel, i);
		}
	}
	
	private void createUpAndDownButton(int x, int y, int floorNumber){
			JButton upButton = new JButton("↑");
			upButton.setMargin(new Insets(0, 0, 0, 0));
			upButton.setBounds(x + floorWidth + 20, y + floorHeight/3, 20, 20);
			this.add(upButton);
			upButton.addActionListener(this);
			upButton.setActionCommand("UP:" + floorNumber);
			if(floorNumber == floors){
				upButton.setEnabled(false);
			}
			upList.add(upButton);
			
			
			JButton downButton = new JButton("↓");
			downButton.setMargin(new Insets(0, 0, 0, 0));
			downButton.setBounds(x + floorWidth + 20 + 20 + 10, 
					y + floorHeight/3, 20, 20);
			this.add(downButton);
			downButton.addActionListener(this);
			downButton.setActionCommand("DOWN:" + floorNumber);
			if(floorNumber == 1)
				downButton.setEnabled(false);
			downList.add(downButton);
	}

	private void createControlButtons() {
		int xPixel;
		int yPixel = startY + 20;
		for (int i = 1; i <= floors; i++) {
			JButton b = new JButton(i + "");
			b.setMargin(new Insets(0, 0, 0, 0));
			
			b.addActionListener(this);
			b.setActionCommand(i + "");
			this.add(b);
			buttonList.add(b);
			xPixel = startX + 170 + (i%2 == 1?0:25);
			yPixel -= (i%2 == 0?0:25);
			b.setBounds(xPixel, yPixel, 20, 20);
		}
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();
		if(command.startsWith("UP")){
			int floorNumber = Integer.valueOf(command.substring(command.indexOf(":") + 1));
			upCommands[floorNumber - 1] = 1;
			upList.get(floorNumber - 1).setEnabled(false);
		}else if(command.startsWith("DOWN")){
			int floorNumber = Integer.valueOf(command.substring(command.indexOf(":") + 1));
			downCommands[floorNumber - 1] = 1;
			downList.get(floorNumber - 1).setEnabled(false);
		}else{
			int floorNumber = Integer.valueOf(command);
			floorCommands[floorNumber - 1] = 1;
			buttonList.get(floorNumber - 1).setEnabled(false);	
		}
	}

	public List<JButton> getFloorList() {
		return floorList;
	}

	public void setFloorList(List<JButton> floorList) {
		this.floorList = floorList;
	}

	public int getCurrentFloor() {
		return currentFloor;
	}

	public void setCurrentFloor(int currentFloor) {
		this.currentFloor = currentFloor;
	}

	public List<JButton> getButtonList() {
		return buttonList;
	}

	public void setButtonList(List<JButton> buttonList) {
		this.buttonList = buttonList;
	}

	public int[] getFloorCommands() {
		return floorCommands;
	}

	public void setFloorCommands(int[] floorCommands) {
		this.floorCommands = floorCommands;
	}

	public int[] getUpCommands() {
		return upCommands;
	}

	public void setUpCommands(int[] upCommands) {
		this.upCommands = upCommands;
	}

	public int[] getDownCommands() {
		return downCommands;
	}

	public void setDownCommands(int[] downCommands) {
		this.downCommands = downCommands;
	}

	public List<JButton> getUpList() {
		return upList;
	}

	public void setUpList(List<JButton> upList) {
		this.upList = upList;
	}

	public List<JButton> getDownList() {
		return downList;
	}

	public void setDownList(List<JButton> downList) {
		this.downList = downList;
	}

	public Direction getDirection() {
		return direction;
	}

	public void setDirection(Direction direction) {
		this.direction = direction;
	}





}
